package com.ds.infrastructure.hyperspace.console.model.request.environment;

import com.ds.infrastructure.hyperspace.console.entity.enums.TbEnvironmentEnum;
import com.ds.infrastructure.hyperspace.console.entity.enums.TbPublicEnum;
import com.ds.infrastructure.hyperspace.console.entity.table.TbEnvironment;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

/**
 * @author: yaozhendong
 * @create: 2019-09-18 11:37
 **/
@Data
@NoArgsConstructor
public class SaveEnvironmentForm {

    private Long parentId;

    @NotBlank
    @Pattern(regexp = "^[a-zA-Z-/0-9]{2,100}$", message = "不满足正则表达式:^[a-zA-Z-/0-9]{2,100}$")
    private String environmentName;
    @NotBlank
    private String packageName;

    private String description;

    public static TbEnvironment toEntity(SaveEnvironmentForm form) {
        TbEnvironment tbEnvironment = new TbEnvironment();
        tbEnvironment.setParentId(form.getParentId() == null ? 0 : Math.max(0, form.getParentId()));
        tbEnvironment.setEnvironmentName(form.getEnvironmentName());
        tbEnvironment.setPackageName(form.getPackageName());
        tbEnvironment.setUsageState(TbEnvironmentEnum.USAGE_STATE.YES.code);
        tbEnvironment.setDescription(form.getDescription());
        tbEnvironment.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        return tbEnvironment;
    }

    public static TbEnvironment toEntity4SelectOne(SaveEnvironmentForm form) {
        TbEnvironment tbEnvironment = new TbEnvironment();
        tbEnvironment.setEnvironmentName(form.getEnvironmentName());
        tbEnvironment.setGmtIsDeleted(TbPublicEnum.GMD_IS_DEL.NO.code);
        return tbEnvironment;
    }
}
